package com.rollawizards.client 
{
    import flash.display.DisplayObject;
    import flash.utils.ByteArray;
    import com.rollawizards.input.InputEvent;
    import com.rollawizards.input.Keyboard;
    import com.rollawizards.input.Mouse;

    public class Client
    {
        private const INPUT_PACKET:int = 0;
        
        private var keyboard:Keyboard;
        private var mouse:Mouse;
        private var proxy:Proxy;

        public function Client(display:DisplayObject, proxy:Proxy)
        {
            keyboard = new Keyboard(display);
            mouse = new Mouse(display);
            this.proxy = proxy;

            keyboard.addEventListener(InputEvent.KEYBOARD, handleInput);
            mouse.addEventListener(InputEvent.MOUSE, handleInput);
            mouse.addEventListener(InputEvent.MOUSEMOVE, handleInput);
        }

        private function handleInput(event:InputEvent):void
        {
            var inputPacket:ByteArray = new ByteArray();
            inputPacket.writeByte(INPUT_PACKET);
            inputPacket.writeBoolean(keyboard.upPressed);
            inputPacket.writeBoolean(keyboard.downPressed);
            inputPacket.writeBoolean(keyboard.leftPressed);
            inputPacket.writeBoolean(keyboard.rightPressed);
            inputPacket.writeBoolean(mouse.down);
            inputPacket.writeShort(mouse.x);
            inputPacket.writeShort(mouse.y);

            proxy.sendPacket(inputPacket);
        }

        public function receivePacket(packet:ByteArray):void
        {
        }
        
        public function setupLocalProxy(localProxy:com.rollawizards.server.LocalProxy)
        {
            proxy = new LocalProxy(localProxy);
        }
    }
}